<?php
namespace Home\Controller;
use Think\Controller;
use Think\Pinyin;
use Home\Model\GoodsClassModel;
 
class IndexController extends CommonController {
	protected $st_store = 'store';//店铺表
	protected $tb_goods = 'product';//产品数据表
	protected $st_class = 'store_class';//店铺分类表
	protected $gc_class = 'goods_class';//商品分类表
	protected $tb_adv = 'adv';//广告表	
	protected $tb_prom = 'promotion';//促销表
	
	
	/*
	*商城首页
	*/	
    public function index(){

		//首页中部广告
		$adv_h1 = S('home_adv_1');

		//首页底部广告
		$adv_h2 = S('home_adv_2');
		
		unset($str);
		unset($data);
		unset($cache);
		$cache = 'home_adv' ;
		$str = 'adv_id>%d and is_allow=%d and adv_type=%d' ;
		$data= array(0,1,11) ;
		//焦点大图 幻灯片
		$adlist_a = _getList($this->tb_adv,$str,$data,$cache,'slide_sort') ;
		unset($data);
		unset($cache);
		$cache = 'home_adv_4' ;		
		$data= array(0,1,1) ;
		//四张联动焦点图 幻灯片
		$adlist_4 = _getList($this->tb_adv,$str,$data,$cache,'slide_sort') ;
		foreach ($adlist_4 as $key=>$v) {
			$v['adv_pic'] = explode('||',$v['adv_pic']) ;
			$v['adv_url'] = explode('||',$v['adv_url']) ;
			$adlist_4 [$key] ['adv_pic']  = $v['adv_pic'] ;
			$adlist_4 [$key] ['adv_url'] = $v['adv_url'] ;
			foreach ($adlist_4 [$key] ['adv_pic'] as $k=>$vv) {
				$adlist_4 [$key] ['ppic'][$k]['pic'] = $vv ;
				$adlist_4 [$key] ['ppic'][$k]['url'] = $adlist_4 [$key] ['adv_url'] [$k] ;
			}
		}
			
		//推荐促销区
		unset($str);
		unset($data);
		unset($cache);
		$cache = 'prom_list_1' ;
		$str = 'se_id>%d and se_type' ;
		$data = array(0,1) ;
		//S($cache,null) ;
		if (S($cache)) {
			$prom_list = S($cache) ;
		} else {
			$prom_list = _getList($this->tb_prom,$str,$data,'','se_id desc') ; 
			foreach($prom_list as $k=>$v) {

				$prom_list [$k]['g_id'] = strAnalysis($v['g_id']) ;
				$prom_list [$k]['number'] = $k ;
				//促销区商品
				foreach ($prom_list [$k]['g_id'] as $key=>$vv) {
					unset($goods) ;
					 $goods = _getOne($this->tb_goods,array('id'=>$vv));
					 $goods ['price'] = number_format($goods ['price'],2) ;
					 $prom_list [$k]['goods'][] = $goods ;
				}	
			}
			S($cache,$prom_list) ;				
		}
		
		//限时折扣区
		unset($xs_list);
		$cache = 'home_xianshi' ;
		//S($cache,null);	
		if (S($cache)) {
			$xs_list = S($cache);
		} else {
			unset($table);
			unset($join);
			unset($field);		
			unset($order);
			unset($str);
			unset($data);
			$order = 'xs.xianshi_id desc';		
			$table = '__P_XIANSHI__ as xs ' ;
			$join ['join1'] = ' LEFT  JOIN __P_XIANSHI_STORE__ as st ON st.xs_id=xs.xianshi_id ' ;
			$join ['join2'] = 'LEFT JOIN __P_XIANSHI_GOODS__ as g ON g.xianshi_id=xs.xianshi_id' ;
			$field = 'g.*' ;	
			$str = "xs.xianshi_type=%d and xs.state=%d and xs.end_time>%d and st.p_state=%d";
			$data = array(1,1,time(),1);
			
			$xs_list = _RowsJoin($table,$join,$str,$data,$field,$order) ;
			foreach ($xs_list as $key => $v) {
				if ($v['xianshi_goods_id']) {
					$xianshi_discount = $v['xianshi_price'] / $v['goods_price'] * 10 ;			
					$xs_list [$key] ['xianshi_discount'] = number_format($xianshi_discount,1) ;unset($xianshi_discount);
					$xs_list [$key] ['end_time'] =	$v['end_time'] - time();						
				} else {
					unset($xs_list);
				}	
			}
			
			S($cache,$xs_list);			
		}
		

		//分类商品楼层区
		unset($goods_list);
		$cache = 'home_goods' ;
		//S($cache,null);
			
		if (S($cache)) {
			$goods_list = S($cache);	
		} else {		
			$order = 'gc_id desc';	
			$where = array(				
				'gc_show' => 1,				 
			);
			//关联查询
			$goods_list= D($this->gc_class)->relation(true)->where($where)->order($order)->select();
			$f = 0 ;
			foreach ($goods_list as $key=>$v) {
				if ($v['goods']) {
					foreach ($v['goods'] as $kk=>$vv) {
						//缩略图
						unset($minpic);	
						if (!$vv['minpic']) {								
							$minpic = thumb_img($vv['pic'], $name = 'goods', $type=2 , $vv, 300,300) ;
						}						
						//dump($minpic);die;
						if ($minpic) {
							$v['goods'] [$kk]['minpic'] = $minpic;
							unset($data);
							$data['minpic'] = $minpic ;
							$deit =  M('product')->where(array('id'=>$vv['id']))->save($data);
						} else {
							if (!$vv['minpic']) {
								$v['goods'] [$kk]['minpic'] = $vv['pic'] ;
							}
						}
						
					}
					
					$goods_list [$key] ['goods'] = $v['goods'] ;
					
					$f = $f + 1 ;
					
					$goods_list [$key] ['floor'] = $f ;
					!$goods_list [$key] ['g_pic'] && $v['goods'][1]['minpic']? $goods_list [$key] ['g_pic'] = $v['goods'][1]['minpic'] : $goods_list [$key] ['g_pic'] = $v['goods'][0]['minpic'];
					
					!$goods_list [$key] ['gg_id'] && $v['goods'][1]['id']? $goods_list [$key] ['gg_id'] = $v['goods'][1]['id'] : $goods_list [$key] ['gg_id'] = $v['goods'][0]['id'];
									
				}
				
				$goods_list [$key] ['gc_son'] = _getList($this->gc_class,'gc_show=%d and gc_parent_id=%d',array(1,$v['gc_id'])) ;
				//楼层颜色
				$f==1 && $goods_list [$key] ['color'] ='style-red';
				$f==2 && $goods_list [$key] ['color'] ='style-pink';
				$f==3 && $goods_list [$key] ['color'] ='style-orange';
				$f==4 && $goods_list [$key] ['color'] ='style-green';
				$f==5 && $goods_list [$key] ['color'] ='style-blue';
				$f==6 && $goods_list [$key] ['color'] ='style-purple';
				$f==7 && $goods_list [$key] ['color'] ='style-brown';
				$f==8 && $goods_list [$key] ['color'] ='style-default';
				
			}
			
			S($cache,$goods_list);			
		}
		//dump($goods_list);die;
		
		$this->assign('prom_list',$prom_list) ;		
		$this->assign('adlist_4',$adlist_4) ;		
		$this->assign('adlist_a',$adlist_a) ;
		$this->assign('xs_list',$xs_list) ;	
		$this->assign('goods_list',$goods_list) ;
		$this->assign('service', $this -> service);
		$this->assign('adv_h1',$adv_h1) ;
		$this->assign('adv_h2',$adv_h2) ;
		
		$this->display();
    }
	
	/**
	*店铺列表
	*/
	public function store_list(){  
	
		$ip = get_client_ip() ;
		
		if (!$_GET['p']) {
			$_GET['p'] = 1;
		} else {
			$p = explode('.',$_GET['p']) ;
			$_GET['p'] = $p[0] ;
		}
/* 		$pin = new Pinyin();;
		$word = $pin->getAllPY(I('get.keyword')) ; */
		
		$cache = 'store_list'.$_GET['p'];
		$cache_pg = 'store_page'.$_GET['p'] ;
					
		if (I('get.select')=='store_list') {
			//店铺搜索			
			$type = 1 ;
			$keyword = 	I('get.keyword') ;		
		} else if (I('get.select')=='sc_class') {
		   //店铺类目搜索		
			$sc_id = $this -> store_cl_search(I('get.keyword')) ;			
			$type = 1 ;	
			$str_j = 'and sc_id=%d';		
		}
			
		if ($type) {
			$cache = 'store_list'.$_GET['p'].I('get.keyword');
			$cache_pg = 'store_page'.$_GET['p'].I('get.keyword') ;	
			if ($sc_id) {
				$keyword = '' ;				
			}
		}
		
		if ($_SESSION['store_search']=='1' && !$type) {				
		}
		
		//强制刷新	
		_Refresh(1,$cache);
		_Refresh(1,$cache_pg);	
		   		
		if (S($cache)) {
			$store_list = S($cache) ;
			$show = S($cache_pg) ;
		} else {			
			unset($order);			
			unset($field);
			unset($str);
			unset($data);		
			$num = 10;
			if ($type) {
				//搜索				
				$order = 'store_id desc';
				$str = "store_id>%d and store_state=%d and store_name LIKE '%s' and area_info LIKE '%s'".$str_j;
				$data = array(0,1,'%'.$keyword.'%','%'.I('get.area_info').'%',$sc_id['sc_id']) ;
				unset($sc_id);
				session('store_search',1);
			} else {
				//全部店铺
				$order = 'store_id desc';
				$str = "store_id>%d and store_state=%d";
				$data = array(0,1) ;				
			}
			
			$field = 'store_id,store_name,seller_name,store_zy,area_info,store_label,store_avatar,store_credit,store_desccredit,store_servicecredit,store_deliverycredit,store_sales,sc_id' ;
			//分页
			$show = Page($this->st_store,$str,$data,$num);	
			//dump($data);
			$store_list = _getList($this->st_store,$str,$data,'',$order,$_GET['p'],$num,$field) ;
			//店铺下的16个产品
			foreach ($store_list as $key=>$vv) {

				unset($str);
				unset($data);
				unset($order);
				unset($field);
				unset($num);
				$num = 16 ;				
				$order = 'id desc';				
				$str = "st_id=%d and goods_state=%d and goods_commend=%d and goods_verify=%d";
				$data = array($vv['store_id'],1,1,1) ;				
				$field = 'productname,id,pic,price,goods_storage,point_num,coin_num' ;	
				$count = 0 ;
				
				$goods_list = _getList($this->tb_goods,$str,$data,'',$order,1,$num,$field) ;

				$count = count($goods_list) ;
				$goods_list && $store_list [$key] ['goods_list'] = $goods_list ;
				
				$store_list [$key] ['goods_count'] = $count ;
				//主营商品
				$store_zy =  strAnalysis($vv ['store_zy']) ;
				$name = '';
				foreach ($store_zy as $zy => $vo) {
					$name .= $vo['name'].',' ;
				}
				if ($name) {
					$name = substr($name,0,-1) ;
				}
								
				$store_list [$key] ['store_zy'] = $name ;
				unset($goods_list);
				unset($count);
				unset($name);
			}
			//保存缓存
			S($cache,$store_list) ;
			S($cache_pg,$show) ;			
		}
		//店铺分类		
		unset($cache);
		unset($order);
		unset($str);
		unset($data);
		//S('st_class',null);
		$cache = 'st_class';
		$order = 'sc_id desc';				
		$str = "sc_name<>'%s'";
		$data = array('') ;		
		$sc_list = _getList($this->st_class,$str,$data,$cache,$order) ;		
		
		$this->assign('sc_list',$sc_list);
		$this->assign('page',$show);
		$this->assign('store_list',$store_list) ;	
		$this->display();   
	}
	
	/*
	*店铺分类查询
	*
	*/	
	private function store_cl_search($str){
		//店铺分类		
		if (S('st_class')) {
			foreach (S('st_class') as $key => $vv) {				
				if ($vv['sc_name']==$str) {
					$result = $vv ;
				}				
			}
		} else {
			$str = "sc_name='%s'";
			$data = array($str) ;		
			$result = _getOne($this->st_class,$str,$data) ;			
		}					
		return $result ;
	}
	
	/*
	*店铺首页
	*
	*/	
	public function store_sp_index(){

		$store = $this->store_info_a($_GET['stid']) ;		
		$pic = '/shop/show_store.html?stid='.$_GET['stid'] ; 
		$url = $_SERVER["HTTP_HOST"].$pic ;	
		
		if ($_GET['png']) {
			//店铺二维码
			$url = createQRcode($url);
		}
		
		$cache_1 = 'host_sell'.$_GET['stid'] ;
		$cache_2 = 'host_collect'.$_GET['stid'] ;		
		$cache_3 = 'st_g_recom'.$_GET['stid'] ;
		$cache_4 = 'st_g_new'.$_GET['stid'] ;
		
		$order_1 = 'goods_salenum desc';
		$order_2 = 'goods_collect desc';	
		$order_3 = 'goods_commend desc';
		$order_4 = 'id desc';	
		
		unset($str);
		unset($data);
		S($cache_1,null) ;
		S($cache_2,null) ;
		S($cache_3,null) ;
		S($cache_4,null) ;
		
		if (S($cache_1)) {
			//热销商品排行
			$host_sell = S($cache_1) ;
		}
		
		if (S($cache_2)) {
			//热门收藏排行
			$host_collect = S($cache_2) ;
		}

		if (S($cache_3)) {
			//推荐商品
			$st_g_recom = S($cache_3) ;
		}
		
		if (S($cache_4)) {
			//新品
			$st_g_new = S($cache_4) ;
		}
		
		unset($field);		
		$num = 5 ;
		$num2 = 12 ;		
		$str = "st_id=%d and goods_state=%d and goods_commend=%d and goods_verify=%d";
		$data = array($_GET['stid'],1,1,1) ;				
		$field = 'productname,id,pic,price,goods_storage,goods_collect,goods_salenum,goods_commend,imgset,point_num,coin_num' ;	
		
		//热销商品排行		
		!$host_sell && $host_sell = _getList($this->tb_goods,$str,$data,$cache_1,$order_1,1,$num,$field) ;		
		foreach ($host_sell as $key=>$v) {
			$host_sell [$key]['imgset'] = strAnalysis($v['imgset']) ;
			
		}
		
		//热门收藏排行
		!$host_collect && $host_collect = _getList($this->tb_goods,$str,$data,$cache_2,$order_2,1,$num,$field) ;
		foreach ($host_collect as $key=>$v) {
			$host_collect [$key]['imgset'] = strAnalysis($v['imgset']) ;
		}		
		
		//推荐商品
		!$st_g_recom && $st_g_recom = _getList($this->tb_goods,$str,$data,$cache_3,$order_3,1,$num2,$field) ;
		foreach ($st_g_recom as $key=>$v) {
			$st_g_recom [$key]['imgset'] = strAnalysis($v['imgset']) ;
			$st_g_recom [$key]['imgset'] =	$st_g_recom [$key]['imgset'] ;
		}	
		
		//新品
		!$st_g_new && $st_g_new = _getList($this->tb_goods,$str,$data,$cache_4,$order_4,1,$num2,$field) ;
		foreach ($st_g_new as $key=>$v) {
			$st_g_new [$key]['imgset'] = strAnalysis($v['imgset']) ;
			$st_g_new [$key]['imgset'] = $st_g_new [$key]['imgset'] ;
		}	
		
		//dump($host_sell);die;
		$this->assign('host_sell',$host_sell);	//热销商品排行
		$this->assign('host_collect',$host_collect);	//热门收藏排行
		$this->assign('st_g_recom',$st_g_recom);	//推荐商品
		$this->assign('st_g_new',$st_g_new);	//新品
		$this->assign('st_picurl',$pic);//店铺地址				
		$this->assign('st_id',$_GET['stid']);//店铺ID
		$this->assign('stores',$store);	//店铺资料
		$this->display();
	}
	
	/*
	*店铺所有商品（店铺内）
	*
	*/		
	public function store_sp_g_all() {
		
		if (!$_GET['p']) {
			$_GET['p'] = 1;
		} else {
			$p = explode('.',$_GET['p']) ;
			$_GET['p'] = $p[0] ;
		}	
		
		$word = 'id' ;		
		$store = $this->store_info_a($_GET['stid'], 1, $word, $_GET['p'],I('get.inkeyword')) ;	
		
		$pic = '/shop/show_store.html?stid='.$_GET['stid'] ; 
		$url = $_SERVER["HTTP_HOST"].$pic ;	
		
		if ($_GET['png']) {
			//店铺二维码
			$url = createQRcode($url);
		}
		
		$this->assign('st_picurl',$pic);//店铺地址				
		$this->assign('st_id',$_GET['stid']);//店铺ID
		$this->assign('stores',$store['store']);	//店铺资料
		$this->assign('goods_list',$store['goods_list']);	//所有商品
		$this->assign('page',$store['show']);	//商品分页
		
		$this->display();		
	}
	
	/*
	*店铺信息和所有商品
	*
	*/		
	private function store_info_a($stid,$type=0,$word='id',$peg,$keyword='') {
		
		$store = array();
		
		unset($str);
		unset($data);
		$str = "store_id=%d and store_state=%d";
		$data = array($stid,1) ;		
		$st_list = _getOne($this->st_store,$str,$data) ;

		if (!$st_list) {
			$this->error('店铺不存在或已关闭','store_list');
		}
		
		if ($type) {
			
			unset($cache);
			unset($cache_peg);
			//dump($keyword);die;
			if ($keyword && $_GET["select"]) {
				$cache = 'st_g_list'.$peg.$stid.$keyword ;
				$cache_peg = 'st_g_peg'.$peg.$stid.$keyword ;				
			} else {
				$cache = 'st_g_list'.$peg.$stid.$word ;
				$cache_peg = 'st_g_peg'.$peg.$stid.$word ;				
			}
			
			S($cache,null) ;
			S($cache_peg,null) ;
			
			if (S($cache)) {
				//热销商品排行
				$goods_list = S($cache) ;
				$show = S($cache_peg) ;
			}
			
			unset($str);
			unset($data);
			unset($field);		
			$num = 20 ;		
						
			if ($keyword && $_GET["select"]) {
				//店铺内产品搜索
				$str = "id>%d and st_id=%d and goods_state=%d and goods_verify=%d and productname LIKE '%s'";
				$data = array(0,$stid,1,1,'%'.$keyword.'%') ;				
			} else {

				$str = "st_id=%d and goods_state=%d and goods_commend=%d and goods_verify=%d";
				$data = array($stid,1,1,1) ;											
			}
			
			$field = 'productname,id,pic,price,goods_storage,goods_collect,goods_salenum,goods_commend,imgset,point_num,coin_num' ;		
			$order = $word.' desc';	
			
			//分页
			!$show && $show = Page($this->tb_goods,$str,$data,$num);
			
			!S($cache_peg) && S($cache_peg, $show) ;
			
			//所有商品		
			!$goods_list && $goods_list = _getList($this->tb_goods,$str,$data,$cache,$order,$peg,$num,$field) ;
			
			foreach ($goods_list as $key=>$v) {
				$goods_list [$key]['imgset'] = strAnalysis($v['imgset']) ;
				$goods_list [$key]['price'] = number_format($v['price'],2) ;
				$goods_list [$key]['imgset'] =	$goods_list [$key]['imgset'];
			}
		
			$store['store'] = $st_list ;
			$store['goods_list'] = $goods_list ;
			$store['show'] = $show ;
		} else {
			$store = $st_list ;
		}
				
		return $store ;	
	}
	
	/*
	*店铺内产品搜索
	*
	*/		
	public function store_g_search($keyword) {
			
	}		

	
}